package com.tgpms.vforms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.vforms.service.OrganService;

@Service
public class OrganServiceImpl implements OrganService {

	@Resource
	private VformInterface vformInterface;

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> selOrganTreeList(String dataowner) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> rootList = new ArrayList<Map<String,Object>>();
		Result result;
		if (StringUtils.isEmpty(dataowner))
			result = vformInterface.find("D1102", "", "1", "10000", "N", "N", null);
		else
			result = vformInterface.find("D1102", "", "1", "10000", "Y", "Y", dataowner);
		System.out.println("D1102机构查询返回的结果为："+result.getData());
		List<Map<String,Object>> rowlist = JSONArray.parseObject(result.getData().toString(), List.class);
		//声明一个map，用来过滤已操作过的数据
		Map<String,Object> map = new HashMap<String, Object>(rowlist.size());
		//循环过滤
		rowlist.stream().filter(c -> c.get("PARENT_CODE")==null || c.get("PARENT_CODE").equals(""))
		.forEach(c -> {
			rootList.add(c);
			map.put(c.get("ENTITY_CODE").toString(),c.get("PARENT_CODE").toString());//当前节点code和父节点id
		});
		rootList.forEach(c -> {
			List<Map<String, Object>> childList = getChild(c, map, rowlist);
			if(childList!=null && childList.size()>0) {
				c.put("children", childList);
			}
			list.add(c);
		});
		return list;
	}

	private List<Map<String, Object>> getChild(Map<String, Object> root,Map<String,Object> map,
			List<Map<String, Object>> rowlist) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		rowlist.stream()
		.filter(c -> !map.containsKey(c.get("ENTITY_CODE")))
		.filter(c -> c.get("PARENT_CODE")!=null)
		.filter(c -> !c.get("PARENT_CODE").equals(""))
		.filter(c -> c.get("PARENT_CODE").equals(root.get("ENTITY_CODE")))
		.forEach(c ->{
			map.put(c.get("ENTITY_CODE").toString(),c.get("PARENT_CODE").toString());//当前节点code和父节点id
			//查询改机构下是否有合同
			JSONObject json = new JSONObject();
			json.put("[SQL1]", " ");
			json.put("[SQL2]", " ");
			json.put("[SQL3]", " AND a.DIVISION='"+c.get("ENTITY_CODE")+"'");
			Result result = vformInterface.multiForMsQueryBySql("SHTDL001", json.toJSONString(), "1", "1000", "N", "N", null);
			JSONObject resObject = JSONObject.parseObject(result.getData().toString());
			System.out.println("D4001通过机构代码返回的结果为："+resObject.get("rows").toString());
			if(resObject.get("rows").toString().equals("[]")) {
				c.put("PO_NO_LIST", "");
			}else {
				c.put("PO_NO_LIST", JSONArray.parseObject(resObject.get("rows").toString(), List.class));
			}
			List<Map<String, Object>> childList = getChild(c, map, rowlist);
			if(childList!=null && childList.size()>0) {
				c.put("children", childList);
			}
			list.add(c);
		});
		return list;
	}

	//查询机构信息
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> selOrganList(Map<String, Object> map) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		JSONObject json = new JSONObject();
		if(map.get("entityType")!=null) {
			json.put("ENTITY_TYPE=", map.get("entityType").toString());
		}
		if(map.get("entityCode")!=null) {
			json.put("ENTITY_CODE=", map.get("entityCode").toString());
		}
		if(map.get("parentCode")!=null) {
			json.put("PARENT_CODE=", map.get("parentCode").toString());
		}
		Result result;
		if (StringUtils.isEmpty(map.get("dataowner").toString()))
			result = vformInterface.find("D1102", json.toJSONString(), map.get("pageNo").toString(),
					map.get("pageSize").toString(), "N", "N", "");
		else
			result = vformInterface.find("D1102", json.toJSONString(), map.get("pageNo").toString(),
					map.get("pageSize").toString(), "Y", "Y", map.get("dataowner").toString());
		System.out.println("D1102机构查询返回的结果为："+result.getData());
		list = JSONArray.parseObject(result.getData().toString(), List.class);
		return list;
	}

}
